package com.kinon.gfcanteen_v1.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.kinon.gfcanteen_v1.R;
import com.kinon.gfcanteen_v1.adapter.OrdersAdapter;
import com.kinon.gfcanteen_v1.bean.BaseBean;
import com.kinon.gfcanteen_v1.bean.OrdersBean;
import com.kinon.gfcanteen_v1.network.CommonSubscriber;
import com.kinon.gfcanteen_v1.network.HttpManage;
import com.kinon.gfcanteen_v1.network.StrCallback;
import com.kinon.gfcanteen_v1.util.RxUtil;
import com.kinon.gfcanteen_v1.util.SPManager;
import com.kinon.gfcanteen_v1.view.activity.OrderDetailActivity;
import com.kinon.gfcanteen_v1.view.base.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2018-01-08.
 */

public class OrderFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    private View mView;
    private TextView tv_title;
    private SmartRefreshLayout smart_order;
    private ListView list_order;

    private int page=3;
    private int offset=0;
    private ArrayList<OrdersBean.Orders> orderList;
    private OrdersAdapter adapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView=inflater.inflate(R.layout.fragment_order_layout,container,false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        tv_title=mView.findViewById(R.id.tv_title);
        smart_order=mView.findViewById(R.id.smart_order);
        list_order=mView.findViewById(R.id.list_order);

        tv_title.setText("订单");
        orderList=new ArrayList<>();
        adapter=new OrdersAdapter(orderList,getActivity());
        list_order.setAdapter(adapter);
        getOrders();
        addListener();

    }

    private void addListener() {
        setSmartRefreshLayout();
        list_order.setOnItemClickListener(this);
    }


    private void getOrders(){
        Call<String> call=HttpManage.getInstance().getOrders
                (SPManager.getInstance().getToken(),page,offset);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Gson gson=new Gson();
                OrdersBean orders=gson.fromJson(data,OrdersBean.class);
                orderList.addAll(orders.getOrders());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent=new Intent(getActivity(), OrderDetailActivity.class);
        intent.putExtra("orderid",orderList.get(i).getId()+"");
        startActivity(intent);
    }

    private void setSmartRefreshLayout(){
        smart_order.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2*1000);
                orderList.clear();
                offset=0;
                getOrders();
            }
        });
        smart_order.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2*1000);
                offset += page;
                getOrders();
            }
        });
    }

}
